package com.sea.app.module.process.domain.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sea.app.common.Entity.Page;
import com.sea.app.common.kit.Strings;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ModelQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by 31417 on 2017/4/29.
 */
@Service
public class ActivitiModelServce {

    @Autowired
    RepositoryService repositoryService;

    /**
     * 获取列表
     * @param page
     * @param category
     */
    public void list(Page<Model> page,String category){
        ModelQuery modelQuery = repositoryService.createModelQuery().latestVersion().orderByLastUpdateTime().desc();
        if (!Strings.on(category).isBlank()){
            modelQuery.modelCategory(category);
        }

        page.setCount(modelQuery.count());
        page.setList(modelQuery.listPage(page.limitStart(), page.limitEnd()));
    }


    /**
     * 创建新模型
     * @param name
     * @param key
     * @param description
     * @param category
     * @return
     */
    public Model create(String name, String key, String description, String category){
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode editorNode = objectMapper.createObjectNode();
        editorNode.put("id", "canvas");
        editorNode.put("resourceId", "canvas");
        ObjectNode properties = objectMapper.createObjectNode();
        properties.put("process_author", "none");
        editorNode.put("properties", properties);
        ObjectNode stencilset = objectMapper.createObjectNode();
        stencilset.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
        editorNode.put("stencilset", stencilset);

        Model modelData = repositoryService.newModel();
        description = Strings.on(description).defaultString().toString();
        modelData.setKey(Strings.on(key).defaultString().toString());
        modelData.setName(name);
        modelData.setCategory(category);
        modelData.setVersion(Integer.parseInt(String.valueOf(repositoryService.createModelQuery().modelKey(modelData.getKey()).count()+1)));

        ObjectNode modelObjectNode = objectMapper.createObjectNode();
        modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
        modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, modelData.getVersion());
        modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
        modelData.setMetaInfo(modelObjectNode.toString());

        repositoryService.saveModel(modelData);
        repositoryService.addModelEditorSource(modelData.getId(), Strings.on(editorNode.toString()).toByte());

        return modelData;
    }






    /**
     * 设置Model category
     * @param id
     * @param category
     */
    public void updateCategory(String id, String category) {
        org.activiti.engine.repository.Model modelData = repositoryService.getModel(id);
        modelData.setCategory(category);
        repositoryService.saveModel(modelData);
    }

    /**
     * 删除模型
     * @param id
     */
    public void delete(String id) {
        repositoryService.deleteModel(id);
    }

}
